package rent.hj.launcher.statusbar;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;

import rent.hj.launcher.R;

/**
 * Created by admin on 2017/11/28.
 */

public class StatusBarView extends FrameLayout {
    private static final String TAG = "StatusBarView";

    private StatusBarImp statusBarImp = new StatusBarImp() {

        @Override
        void onIUpdateHeadset(boolean hasMicrophone, boolean isActive) {

        }

        @Override
        void onIUpdateVolumeZen() {

        }

        @Override
        void onIUpdateBluetooth(boolean isEnable, boolean isConnected, int batteryValue) {
            View view = findViewById(R.id.bluetooth_img);
            if (isEnable) {
                view.setVisibility(VISIBLE);
                if (isConnected) {
                    view.setAlpha(1);
                } else {
                    view.setAlpha(0.5f);
                }
            } else {
                view.setVisibility(GONE);
            }
        }

        @Override
        void onIUpdateWiFi(boolean isEnable, boolean isConnected, int RSSI) {

            View view = findViewById(R.id.wifi_img);

            if (isEnable) {
                if (isConnected) {
                    if (RSSI < 3) {
                        view.setBackgroundResource(R.drawable.wifi_1);
                    } else if (RSSI < 6) {
                        view.setBackgroundResource(R.drawable.wifi_2);
                    } else {
                        view.setBackgroundResource(R.drawable.wifi_3);
                    }
                }
                view.setVisibility(VISIBLE);
            } else {
                view.setVisibility(GONE);
            }
        }

        @Override
        void onIUpdateStorage(String state) {

        }

        @Override
        void onIUpdateBattery(boolean isCharging, int percent) {
            TextView battery_txt = (TextView) findViewById(R.id.battery_txt);
            BatteryView battery_img = (BatteryView) findViewById(R.id.battery_img);
            View lightning_img = findViewById(R.id.lightning_img);

            battery_txt.setText(percent + "%");
            battery_img.setBatteryPercent(percent);

            if (isCharging) {
                lightning_img.setVisibility(View.VISIBLE);
                battery_img.setColor(Color.GREEN);
            } else {
                lightning_img.setVisibility(View.GONE);
                if (percent > 20) {
                    battery_img.setColor(Color.WHITE);
                } else {
                    battery_img.setColor(Color.RED);
                }
            }
        }

        @Override
        void onIUpdateTime(String time) {
            TextView textView = (TextView) findViewById(R.id.time_txt);
            textView.setText(time);
        }

        @Override
        void onIUpdateAirplane(boolean isAirplane) {
            if (isAirplane) {
                findViewById(R.id.airplane_img).setVisibility(View.VISIBLE);
            } else {
                findViewById(R.id.airplane_img).setVisibility(View.GONE);
            }
        }

        @Override
        void onIUpdateUSB(boolean isInsert) {
            if (isInsert) {
                findViewById(R.id.usd_img).setVisibility(View.VISIBLE);
            } else {
                findViewById(R.id.usd_img).setVisibility(View.GONE);
            }
        }

        @Override
        void onIUpdateSDCard(boolean isInsert) {

        }
    };

    public StatusBarView(@NonNull Context context) {
        super(context);
        init();
    }

    public StatusBarView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        statusBarImp.setContext(getContext());
        addView(View.inflate(getContext(), R.layout.status_bar_view_01, null));
    }

    public StatusBar asStatusBar() {
        return statusBarImp;
    }

    @Override
    public void setLayoutParams(ViewGroup.LayoutParams params) {
        params.width = -2;
        super.setLayoutParams(params);
    }
}
